﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using JM.JCY.Common;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.News.SearchNews
{
    public partial class SearchNewsUserControl : UserControl
    {

        public string _category
        {
            set { ViewState["_category"] = value; }
            get { return (string)ViewState["_category"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CommonFunction.IsApplicationAdmin(9))
                    lkbCreateNews.Visible = true;
                else
                    lkbCreateNews.Visible = false;

                if (Request.QueryString["Category"] != null)
                {
                    _category = Request.QueryString["Category"].ToString();

                    this.hfCategory.Value = _category;
                    if (_category == "News")
                    {

                        //this.lblNewsCategory.Text = "新闻中心";
                        //this.lkbManageNews.Text = "管理新闻";

                        this.lblDisplay.Text = "搜索新闻";
                    }
                    else
                    {

                        //this.lblNewsCategory.Text = "领导讲话";
                        //this.lkbManageNews.Text = "管理领导讲话";

                        this.lblDisplay.Text = "搜索领导讲话";
                    }
                }
            }

        }

        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
            DateTime? start = null;
            DateTime? end = null;
            string subject = this.txtSubject.Text.Trim();

            if (!dtEffDateFrom.IsDateEmpty)
                start = this.dtEffDateFrom.SelectedDate;
           
            if (!dtEffDateTo.IsDateEmpty)
                end = this.dtEffDateTo.SelectedDate;

            using (NewsProxy proxy = new NewsProxy())
            {
                NewsDataSet.T_AnnouncementDataTable newsDT = proxy.GetNews(subject, _category, JCYConstant.Status.Publish, start, end);
                foreach (var item in newsDT)
                {
                    item.ImageUrl = SPContext.Current.Site.ServerRelativeUrl + item.ImageUrl;
                    newsDT.AcceptChanges();
                }
                this.gvBC.DataSource = newsDT;
                this.gvBC.DataBind();
            }
        }

        protected void gvBC_DataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblEffDate = (Label)e.Row.Cells[0].FindControl("lblEffDate");

                string dateString = lblEffDate.Text;
                DateTime dateTime = DateTime.Parse(dateString);
                lblEffDate.Text = dateTime.ToString(JCYConstant.Date_Format_CN);

            }

        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(SPContext.Current.Site.ServerRelativeUrl);
        }
    }
}
